package com.chookss.home.audit.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.StaticClass;
import com.chookss.home.entity.ProcessEntity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.Utils;
import com.chookss.view.CircleImageView;
import com.johnrambo.ktea.ktExtends.ShareUtils;

import java.util.List;

public class ClockInProcessAdapter extends BaseQuickAdapter<ProcessEntity, BaseViewHolderEx> {
    private Activity context;
    private int type;//3补卡 4请假7报销9借款10补贴

    public ClockInProcessAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<ProcessEntity> data, int type) {
        super(layoutResId, data);
        this.context = context;
        this.type = type;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, ProcessEntity entity) {
        ImageView ivType = helper.getView(R.id.ivType);
        CircleImageView ivHead = helper.getView(R.id.ivHead);
        View viewTop = helper.getView(R.id.viewTop);
        View viewBottom = helper.getView(R.id.viewBottom);
        TextView tvTitle = helper.getView(R.id.tvTitle);
        TextView tvName = helper.getView(R.id.tvName);
        TextView tvTitleInit = helper.getView(R.id.tvTitleInit);
        TextView tvStatus = helper.getView(R.id.tvStatus);
        TextView tvTime = helper.getView(R.id.tvTime);
        TextView tvAudit = helper.getView(R.id.tvAudit);
        LinearLayout llTop = helper.getView(R.id.llTop);
        LinearLayout parentLayout = helper.getView(R.id.parentLayout);
        if(Utils.isNull(entity.getRemark())){
            tvAudit.setVisibility(View.GONE);
        }else{
            tvAudit.setVisibility(View.VISIBLE);
            tvAudit.setText("审核说明："+entity.getRemark());
        }
        if (entity.isEndProcess()) {
            //已结束
            ivType.setBackgroundResource(R.drawable.kq_sptg_icon);
            if (!Utils.isNull(entity.getRealAuditHeadPhotoThumbUrl())) {
                GlideUtils.loadAvatar(context, entity.getRealAuditHeadPhotoThumbUrl(), ivHead);
            } else if (!Utils.isNull(entity.getHeadPhotoThumbUrl())) {
                GlideUtils.loadAvatar(context, entity.getHeadPhotoThumbUrl(), ivHead);
            } else {
                GlideUtils.loadAvatar(context, new ShareUtils().getString(context, StaticClass.ORIGINALURL, ""), ivHead);
            }
            if (helper.getItemPosition() == getData().size() - 1) {
                viewBottom.setVisibility(View.GONE);
                viewTop.setVisibility(View.VISIBLE);
            } else {
                viewBottom.setVisibility(View.VISIBLE);
                viewTop.setVisibility(View.VISIBLE);
            }
            tvTime.setText("");
            viewTop.setBackgroundColor(Color.parseColor("#ECECEC"));
            viewBottom.setBackgroundColor(Color.parseColor("#ECECEC"));
            llTop.setVisibility(View.GONE);
            tvStatus.setText("已结束");
            tvStatus.setTextColor(Color.parseColor("#00C993"));
        } else {
            llTop.setVisibility(View.VISIBLE);
            tvTime.setText(Utils.getAccurateDate(entity.getAuditTime()));
            tvName.setText(entity.getRealAuditEmployeeName());
            //getWorkFlowName  环节名称
            //getAuditEmployeeName  项目组
            //getRealAuditEmployeeName   真实审批人
            // connectType //0发起人1行政审批  只有0和1不展示（）中的项目名称，其余的需要展示
            //格式 环节名称（项目名称）
            if (!Utils.isNull(entity.getRealAuditHeadPhotoThumbUrl())) {
                GlideUtils.loadAvatar(context, entity.getRealAuditHeadPhotoThumbUrl(), ivHead);
            } else {
                GlideUtils.loadAvatar(context, entity.getAuditHeadPhotoThumbUrl(), ivHead);
            }
            if ("0".equals(entity.getConnectType()) || "1".equals(entity.getConnectType())) {
                tvTitle.setText(entity.getWorkFlowName());
                if ("0".equals(entity.getConnectType())) {
                    if (Utils.isNull(entity.getRealAuditEmployeeName())) {
                        tvName.setText(entity.getCreateEmployeeName());
                    }
                }
            } else {
                if (Utils.isNull(entity.getWorkFlowName())) {
                    tvTitle.setText(entity.getWorkFlowName());
                    tvTitleInit.setText(entity.getAuditEmployeeName());
                } else {
                    if (Utils.isNull(entity.getAuditEmployeeName())) {
                        tvTitle.setText(entity.getWorkFlowName());
                        tvTitleInit.setText("");
                    } else {
                        tvTitle.setText(entity.getWorkFlowName());
                        tvTitleInit.setText("(" + entity.getAuditEmployeeName() + ")");
                    }
                }
            }
            if (helper.getItemPosition() == 0) {
                if (type == 3) {
                    tvStatus.setText("发起补卡申请");
                } else if (type == 4) {
                    tvStatus.setText("发起请假申请");
                } else if (type == 7) {
                    tvStatus.setText("发起报销申请");
                } else if (type == 8) {
                    if ("5".equals(entity.getParams().getStatusCd())) {
                        tvStatus.setText("发起物品遗失申请");
                    } else if ("6".equals(entity.getParams().getStatusCd())) {
                        tvStatus.setText("发起物品报废申请");
                    } else if ("7".equals(entity.getParams().getStatusCd())) {
                        tvStatus.setText("发起物品交付申请");
                    } else if ("8".equals(entity.getParams().getStatusCd())) {
                        tvStatus.setText("发起物品维修申请");
                    } else {
                        tvStatus.setText("发起物品申请");
                    }
                } else if (type == 9) {
                    tvStatus.setText("发起借款申请");
                } else if (type == 10) {
                    tvStatus.setText("发起补贴申请");
                } else if (type == 11) {
                    tvStatus.setText("发起用章申请");
                } else if (type == 12) {
                    tvStatus.setText("发起物品维修申请");
                } else if (type == 13) {
                    tvStatus.setText("发起物品交付申请");
                } else if (type == 14) {
                    tvStatus.setText("发起离职申请");
                } else if (type == 15) {
                    tvStatus.setText("发起晋升申请");
                } else if (type == 16) {
                    tvStatus.setText("发起项目回款申请");
                } else if (type == 17) {
                    tvStatus.setText("发起项目金额调整申请");
                } else if (type == 18) {
                    tvStatus.setText("发起入职申请");
                } else {
                    tvStatus.setText("发起外勤申请");
                }
                tvStatus.setTextColor(Color.parseColor("#777777"));
                ivType.setBackgroundResource(R.drawable.kq_sptg_icon);
            } else {
                //1审核通过 2审核未通过0未审核3废弃
                if ("0".equals(entity.getAuditStatus())) {
                    if ("0".equals(entity.getConnectType())) {
                        tvStatus.setText("待修改");
                    } else {
                        if (Utils.isNull(entity.getAuditEmployeeName())) {
                            tvStatus.setText("待审批");
                        } else {
                            tvTitleInit.setText("");
                            tvStatus.setText("待" + entity.getAuditEmployeeName() + "审批");
                        }

                    }
                    tvStatus.setTextColor(Color.parseColor("#FCA817"));
                    ivType.setBackgroundResource(R.drawable.kq_wsp);
                } else if ("1".equals(entity.getAuditStatus())) {
                    if ("0".equals(entity.getConnectType())) {
                        if (type == 8) {
                            if ("5".equals(entity.getParams().getStatusCd())) {
                                tvStatus.setText("发起物品遗失申请");
                            } else if ("6".equals(entity.getParams().getStatusCd())) {
                                tvStatus.setText("发起物品报废申请");
                            } else if ("7".equals(entity.getParams().getStatusCd())) {
                                tvStatus.setText("发起物品交付申请");
                            } else if ("8".equals(entity.getParams().getStatusCd())) {
                                tvStatus.setText("发起物品维修申请");
                            } else {
                                tvStatus.setText("重新提交");
                            }
                        } else if (type == 9) {
                            if ("1".equals(entity.getParams().getRepaymentCd())) {
                                tvStatus.setText("发起还款申请");
                            } else {
                                tvStatus.setText("重新提交");
                            }
                        } else if (type == 13) {
                            tvStatus.setText("发起物品交付申请");
                        } else {
                            tvStatus.setText("重新提交");
                        }
                    } else {
                        if ("1".equals(entity.getReplaceCd())) {
                            //超管待审
                            tvStatus.setText("超管代审批通过");
                        } else {
                            tvStatus.setText("审批通过");
                        }

                    }
                    tvStatus.setTextColor(Color.parseColor("#00C993"));
                    ivType.setBackgroundResource(R.drawable.kq_sptg_icon);
                } else {
                    if ("3".equals(entity.getAuditStatus())) {
                        tvStatus.setText("废弃");
                    } else {
                        if ("1".equals(entity.getReplaceCd())) {
                            //超管待审
                            tvStatus.setText("超管代审批不通过");
                        } else {
                            tvStatus.setText("审批不通过");
                        }
                    }
                    tvStatus.setTextColor(context.getResources().getColor(R.color.main_color));
                    ivType.setBackgroundResource(R.drawable.icon_red_delete_48);
                }
            }
            parentLayout.post(new Runnable() {
                @Override
                public void run() {
                    int parentHeight = parentLayout.getHeight();
                    if(parentHeight>Utils.dip2px(context,70)){
                        viewBottom.getLayoutParams().height = parentHeight-Utils.dip2px(context,45);
                        viewBottom.requestLayout();
                        viewTop.getLayoutParams().height = parentHeight-Utils.dip2px(context,45);
                        viewTop.requestLayout();
                    }
                }
            });
            if (helper.getItemPosition() == 0) {
                //第一个
                viewBottom.setVisibility(View.VISIBLE);
                viewTop.setVisibility(View.GONE);
                viewBottom.setBackgroundColor(Color.parseColor("#ECECEC"));
            } else if (helper.getItemPosition() + 1 == getData().size()) {
//最后一个
                viewBottom.setVisibility(View.GONE);
                viewTop.setVisibility(View.VISIBLE);
                viewTop.setBackgroundColor(Color.parseColor("#ECECEC"));
            } else {
                viewTop.setVisibility(View.VISIBLE);
                viewBottom.setVisibility(View.VISIBLE);
                viewTop.setBackgroundColor(Color.parseColor("#ECECEC"));
                viewBottom.setBackgroundColor(Color.parseColor("#ECECEC"));
            }
        }
    }
}